﻿using Atlanchim.Crm.Mui.Pages.Societes;
using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Atlanchim.Crm.Mui
{
    public class ModelHome : INotifyPropertyChanged
    {
        private int _clientsCount;
        public int ClientsCount
        {
            get { return _clientsCount; }
            set
            {
                this._clientsCount = value;
                OnPropertyChanged("ClientsCount");
            }
        }

        private int _prospectsCount;
        public int ProspectsCount
        {
            get { return _prospectsCount; }
            set
            {
                this._prospectsCount = value;
                OnPropertyChanged("ProspectsCount");
            }
        }

        private int _contactsCount;
        public int ContactsCount
        {
            get { return _contactsCount; }
            set
            {
                this._contactsCount = value;
                OnPropertyChanged("ContactsCount");
            }
        }

        private int _actionsCount;
        public int ActionsCount
        {
            get { return _actionsCount; }
            set
            {
                this._actionsCount = value;
                OnPropertyChanged("ActionsCount");
            }
        }

        private int _alertesCount;
        public int AlertesCount
        {
            get { return _alertesCount; }
            set
            {
                this._alertesCount = value;
                OnPropertyChanged("AlertesCount");
            }
        }

        private int _dayAlertesCount;
        public int DayAlertesCount
        {
            get { return _dayAlertesCount; }
            set
            {
                this._dayAlertesCount = value;
                OnPropertyChanged("DayAlertesCount");
            }
        }

        private int _weekAlertesCount;
        public int WeekAlertesCount
        {
            get { return _weekAlertesCount; }
            set
            {
                this._weekAlertesCount = value;
                OnPropertyChanged("WeekAlertesCount");
            }
        }

        public ModelHome()
        {
        }

        public void Load()
        {
            using (CrmContext context = new CrmContext())
            {
                try
                {
                    string sTypeClient = ETypeSociete.Client.ToString();
                    string sTypeProspect = ETypeSociete.Prospect.ToString();
                    this.ClientsCount = context.Societes.Where(s => s.Type == sTypeClient).Count();
                    this.ProspectsCount = context.Societes.Where(s => s.Type == sTypeProspect).Count();
                    this.ContactsCount = context.Contacts.Count();
                    this.ActionsCount = context.Actions.Count();

                    // Alertes
                    DateTime todayDate = DateTime.Today;
                    int thisDayOfWeek = (int)DateTime.Today.DayOfWeek;
                    DateTime thisMonday = DateTime.Today.AddDays(thisDayOfWeek - 1);
                    DateTime thisFriday = DateTime.Today.AddDays(thisDayOfWeek - 1 + 5);

                    this.AlertesCount = context.Alertes.Count();
                    this.DayAlertesCount = context.Alertes.Where(alerte => alerte.Date == todayDate).Count();
                    this.WeekAlertesCount = context.Alertes.Where(alerte => (thisMonday <= alerte.Date) && (alerte.Date <= thisFriday)).Count();
                }
                catch
                {
                    //TODO : Logger
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
